//-----------------------------------------------------------------------
// <copyright file="EndpointPresence.cs" company="Microsoft">
//     Copyright (c) Microsoft Corporation.  All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

namespace Microsoft.Live.Messenger
{
    using System;
    using System.ComponentModel;
    using Microsoft.Live.Core;
    using Microsoft.Live.Messenger.Presence;

    /// <summary>
    /// Represents presence information associated with an endpoint.
    /// </summary>
    public sealed class EndpointPresence : INotifyPropertyChanged
    {
        private readonly MessengerEntity entity;
        private readonly Endpoint endpoint;

        private PresencePropertyCollection properties;

        internal EndpointPresence(object obj, Endpoint endpoint)
        {
            this.entity = new MessengerEntity(obj);
            this.entity.AttachEvent("PropertyChanged", this.OnPropertyChanged);

            this.endpoint = endpoint;
        }

        /// <summary>
        /// Gets the properties associated with the endpoint.
        /// </summary>
        public PresencePropertyCollection Properties
        {
            get
            {
                if (this.properties == null)
                {
                    this.properties = new PresencePropertyCollection(this.entity.GetProperty("Properties"));
                }
                return this.properties;
            }
        }

        /// <summary>
        /// Gets the custom properties associated with the endpoint.
        /// </summary>
        public PresenceExtensionCollection Extensions
        {
            get { return null; }
        }

        #region INotifyPropertyChanged Members

        /// <summary>
        /// Occurs when a property value changes.
        /// </summary>
        public event PropertyChangedEventHandler PropertyChanged;

        #endregion

        private void OnPropertyChanged(object sender, MessengerEventArgs e)
        {
            this.OnPropertyChanged(
                new PropertyChangedEventArgs(
                    e.Entity.GetProperty<string>("PropertyChanged")));
        }

        private void OnPropertyChanged(PropertyChangedEventArgs e)
        {
            if (this.PropertyChanged != null)
            {
                this.PropertyChanged(this, e);
            }
        }
    }
}